#!/usr/bin/env node

// We need this to build our post string
var querystring = require('querystring');
var http = require('http');
var fs = require('fs');

function login(codestring) {
    // Build the post string from an object
    var post_data = querystring.stringify({
        'corpCode': 'tbc.com',
        'loginName': 'yuanyuan1',
        'password': '******'
    });

    // An object of options to indicate where to post to
    var post_options = {
        host: 'v4.21tb.com',
        port: '80',
        path: '/login/login.ajaxLogin.do',
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': Buffer.byteLength(post_data)
        }
    };

    // Set up the request
    var post_req = http.request(post_options, function (res) {
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            console.log('Response: ' + chunk);
            var result = JSON.parse(chunk);

            if (result.state === 'S') {
                console.log('登录成功！');
            } else {
                console.log('登录失败！');
            }

        });
    });

    // post the data
    post_req.write(post_data);
    post_req.end();
}

setInterval(function () {
    login();
}, 10 * 1000);